package cn.yunyichina.provider.pay.service.impl;

import cn.yunyichina.provider.framework.dubbo.service.AlipayService;
import cn.yunyichina.provider.iface.entity.common.ResCommon;
import cn.yunyichina.provider.iface.entity.common.ResponseObject;
import cn.yunyichina.provider.iface.entity.dto.pay.AlipayBillDownloadRequestDTO;
import cn.yunyichina.provider.iface.entity.dto.pay.AlipayBillDownloadResponseDTO;
import cn.yunyichina.provider.iface.entity.pay.PayCode;
import cn.yunyichina.provider.pay.constant.ResponseEnum;
import cn.yunyichina.provider.pay.exception.PaySettingNotFoundException;
import cn.yunyichina.provider.pay.service.BaseTradeBillService;
import cn.yunyichina.provider.pay.service.TradeBillService;
import cn.yunyichina.utils.convert.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Lullaby on 2017/7/11.
 */
@Service
public class TradeBillServiceImpl extends BaseTradeBillService implements TradeBillService {

    private static final Logger logger = LoggerFactory.getLogger(TradeBillServiceImpl.class);

    /**
     * 下载对账单
     *
     * @param data
     * @return
     */
    public ResponseObject downloadTradeBill(String data) {
        logger.info("TradeBillServiceImpl.downloadTradeBill.data", data);

        Map<String, String> params = JsonUtils.parseObject(data, Map.class);
        if (StringUtils.isEmpty(params.get("pay_code")) ||
                StringUtils.isEmpty(params.get("app_id")) ||
                StringUtils.isEmpty(params.get("bill_date")))
            return invalidParamsResponseObject();

        try {
            String payCode = params.get("pay_code");
            switch (PayCode.valueOf(payCode)) {
                case WECHAT:
                    return downloadWechatTradeBill(params);
                case ALIPAY:
                    return downloadAlipayTradeBill(params);
                default:
                    return new ResponseObject().buildResultCodeAndMessage(
                            ResponseEnum.UNKNOWN_PAY_CODE.getCode(),
                            ResponseEnum.UNKNOWN_PAY_CODE.getMessage()
                    );
            }
        } catch (PaySettingNotFoundException e) {
            logger.error("TradeBillServiceImpl.downloadTradeBill.PaySettingNotFoundException", e);
            return new ResponseObject().buildResultCodeAndMessage(
                    ResponseEnum.PAY_SETTING_NOT_FOUND.getCode(),
                    ResponseEnum.PAY_SETTING_NOT_FOUND.getMessage()
            );
        } catch (Exception e) {
            logger.error("TradeBillServiceImpl.downloadTradeBill.Exception", e);
            return new ResponseObject().buildResultCodeAndMessage(
                    ResponseEnum.ORDER_QUERY_EXCEPTION.getCode(),
                    ResponseEnum.ORDER_QUERY_EXCEPTION.getMessage()
            );
        }
    }

    /**
     * 下载微信对账单
     *
     * @param params
     * @return
     */
    private ResponseObject downloadWechatTradeBill(Map<String, String> params) throws PaySettingNotFoundException {
        return null;
    }

    /**
     * 下载支付宝对账单
     *
     * @param params
     * @return
     */
    private ResponseObject downloadAlipayTradeBill(Map<String, String> params) throws PaySettingNotFoundException {
        AlipayBillDownloadRequestDTO billRequest = buildAlipayBillDownloadParameters(params);
        logger.info("TradeBillServiceImpl.downloadAlipayTradeBill.billRequest -> {}", JsonUtils.toJsonString(billRequest));

        ResCommon<AlipayBillDownloadResponseDTO> billResponse = AlipayService.getAlipayClient().downloadAlipayBill(billRequest);
        logger.info("TradeBillServiceImpl.downloadAlipayTradeBill.billResponse -> {}", JsonUtils.toJsonString(billResponse));

        AlipayBillDownloadResponseDTO responseDTO = billResponse.getResult();
        AlipayBillDownloadResponseDTO.AlipayDataDataserviceBillDownloadurlQueryResponse
                downloadResponse = responseDTO.getAlipayDataDataserviceBillDownloadurlQueryResponse();
        if (billResponse.isSuccess()) {
            Map<String, String> result = new HashMap<>();
            result.put("download_url", downloadResponse.getBillDownloadUrl());

            return new ResponseObject().buildResultCodeAndMessageAndResult(
                    billResponse.getResultCode(), billResponse.getResultMessage(), result
            );
        }

        return new ResponseObject().buildResultCodeAndMessageAndResult(
                billResponse.getResultCode(), billResponse.getResultMessage(), downloadResponse
        );
    }

}
